package vip.liux.front.application.serviceimpl.chat;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import vip.liux.front.application.service.chat.IRealTimeChatMessageSender;
import vip.liux.front.application.service.chat.dtos.messages.ChatMessageEto;
import vip.liux.front.application.service.chat.dtos.messages.ChatMessagedDto;

@Service
public class DistributedEventBusRealTimeChatMessageSender implements IRealTimeChatMessageSender {
    private final RabbitTemplate rabbitTemplate;

    public DistributedEventBusRealTimeChatMessageSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void sendChatMessage(Long targetUserId, ChatMessagedDto message) {
        rabbitTemplate.convertAndSend(
                ChatMessageEto.class.getName(),
                new ChatMessageEto(
                        targetUserId,
                        message.senderUsername(),
                        message.senderName(),
                        message.senderSurname(),
                        message.senderUserId(),
                        message.text()
                )
        );
    }
}
